<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.0.0.042823
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2023/5/16
 * Time: 13:31
 */

namespace app\model;

use think\Model;

class AccountBank extends Model
{

    // 定义时间戳字段名
    protected $createTime = 'add_time';

    protected $updateTime = 'update_time';

    public $hidden = [
        'status'
    ];

    /**
     * 关联司机表
     * @return \think\model\relation\BelongsTo
     */
    public function driver()
    {
        return $this->belongsTo(Driver::class, 'user_id')->bind(['user_name' => 'name']);
    }

    /**
     * 关联公司表
     * @return \think\model\relation\BelongsTo
     */
    public function company()
    {
        return $this->belongsTo(Company::class, 'user_id')->bind(['user_name' => 'name']);
    }

    /**
     * 获取列表
     * @param $userid
     * @param $type
     * @return AccountBank|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($userid, $type)
    {
        $sql = $this;
        switch ($type) {
            case 1:
                $sql = $sql->with(['driver']);
                break;
            case 2:
                $sql = $sql->with(['company']);
                break;
        }
        return $sql->where('user_id', $userid)
                   ->where('user_type', $type)
                   ->select()
                   ->append(['bank_type_text', 'user_type_text']);
    }


    /**
     * 构造器
     * @param $value
     * @return string
     */
    public function getBankTypeTextAttr($value, $data)
    {
        $types = [1 => '中国银行', 2 => '中国建设银行', 3 => '中国工商银行', 4 => '中国农业银行', 5 => '中国交通银行'];
        return $types[$data['bank_type']];
    }


    /**
     * 构造器
     * @param $value
     * @return string
     */
    public function getUserTypeTextAttr($value, $data)
    {
        $types = [1 => '司机', 2 => '公司', 3 => '平台', 4 => '渠道'];
        return $types[$data['user_type']];
    }

}